package com.cpi.cornpp.biweeks.services;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.biweek.exception.BiWeekException;
import com.cpi.cornpp.biweeks.converter.BiWeekConverter;
import com.cpi.cornpp.biweeks.dao.BiWeekDAO;
import com.cpi.cornpp.biweeks.dto.BiWeekDTO;
import com.cpi.cornpp.biweeks.view.BiWeekView;
import com.cpi.cornpp.biweeks.view.BiWeekViewList;

public class BiWeekService {
	
	public BiWeekViewList getBiWeeks(int versionId) throws BiWeekException{
		BiWeekDAO biWeekDAO = null;				
		List biWeeksDTO = null;
		BiWeekViewList biWeeksView = null;
		try{
			biWeeksView = new BiWeekViewList(0);
			biWeekDAO = new BiWeekDAO();
			biWeeksDTO = biWeekDAO.getBiWeeks(versionId);
			if (!CollectionUtils.isEmpty(biWeeksDTO)){
				biWeeksView = BiWeekConverter.convertDTOList(biWeeksDTO);
			}
		} catch(BiWeekException tre){
			throw tre;
		} catch(Exception e){
			BiWeekException tre = new BiWeekException(e, BiWeekException.LAYER_SERVICE, BiWeekException.ACTION_LISTS);
			e.printStackTrace(System.out);
			throw tre;
		}
		return biWeeksView;
	}
	
	public BiWeekViewList getBiWeeks() throws BiWeekException{
		BiWeekDAO biWeekDAO = null;				
		List biWeeksDTO = null;
		BiWeekViewList biWeeksView = null;
		
		try{
			
			biWeeksView = new BiWeekViewList(0);
			biWeekDAO = new BiWeekDAO();
			
			
			biWeeksDTO = biWeekDAO.getBiWeeks();
			
			if (!CollectionUtils.isEmpty(biWeeksDTO)){
				biWeeksView = BiWeekConverter.convertDTOList(biWeeksDTO);
			}
				
		} catch(BiWeekException tre){
			throw tre;
		} catch(Exception e){
			BiWeekException tre = new BiWeekException(e, BiWeekException.LAYER_SERVICE, BiWeekException.ACTION_LISTS);
			
			e.printStackTrace(System.out);
			
			throw tre;
		}
		
		return biWeeksView;
	}
	
	public BiWeekView getBiWeek(Integer biWeekId) throws BiWeekException{
		BiWeekDTO biWeekDTO = null;
		BiWeekView biWeekView = null;
		BiWeekDAO biWeekDAO = null;
		
		try{
			
			if (biWeekId != null){
				biWeekDAO = new BiWeekDAO();
				
				biWeekDTO = biWeekDAO.getBiWeekById(biWeekId);
				
				if (biWeekDTO != null){
					biWeekView = BiWeekConverter.convert(biWeekDTO);
				}
			}
			
		} catch(BiWeekException bwe){
			throw bwe;
		} catch(Exception e){
			BiWeekException bwe = new BiWeekException(e, BiWeekException.LAYER_SERVICE, BiWeekException.ACTION_SELECT);
			
			e.printStackTrace(System.out);
			
			throw bwe;
		}
		
		return biWeekView;
	}
}
